package tt.dz.order.util;

import java.io.FileInputStream;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.Message.RecipientType;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.util.ByteArrayDataSource;

/**
 * 创建一个复杂的邮件，这个邮件包括一段正文和两个附件
 * 其中正文要包含一张图片
 *
 * @author Administrator
 */
public class MailUtils {

    public static void main(String[] args) throws Exception {
//        try {
//            Calendar nowCalendar = Calendar.getInstance();
//            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
//            nowCalendar.add(Calendar.DAY_OF_MONTH, -1);
//            String startDatestr = dateFormat.format(nowCalendar.getTime());
//            nowCalendar.setTime(dateFormat.parse(startDatestr));
//            Date startDate = nowCalendar.getTime();
//            System.out.println(startDatestr);
//            System.out.println(startDate);
////		OrderAlreadyPaidVo.queryByDate();
//        }catch (Exception e){
//            e.printStackTrace();
//        }
    }

    public static void sendMail(String tomail, String contentStr, String fileNamePath, String fileName) throws Exception {
        Properties props = new Properties();
        props.setProperty("mail.smtp.auth", "true");
        props.setProperty("mail.transport.protocol", "smtp");

        Session session = Session.getInstance(props);
        session.setDebug(true);

        Message msg = new MimeMessage(session);
        /*邮件消息头设置*/
        msg.setFrom(new InternetAddress("titans@haotejt.com"));
        msg.setRecipients(RecipientType.TO, InternetAddress.parse(tomail));
        msg.setSubject("豪特今日账单");

        /*邮件消息内容设置，包括两个附件和一段正文*/
        Multipart msgPart = new MimeMultipart("mixed");
        msg.setContent(msgPart);

        MimeBodyPart body = new MimeBodyPart();     //表示正文
        MimeBodyPart attach1 = new MimeBodyPart();  //表示附件1
//        MimeBodyPart attach2 = new MimeBodyPart();  //表示附件2
        msgPart.addBodyPart(body);
        msgPart.addBodyPart(attach1);
//        msgPart.addBodyPart(attach2);

        /*以下为设置正文*/
        /*正文是文字和图片混合的*/
        Multipart contentPart = new MimeMultipart("related");
        body.setContent(contentPart);
        MimeBodyPart content = new MimeBodyPart(); //文字
//        MimeBodyPart img = new MimeBodyPart();     //图片
        contentPart.addBodyPart(content);
//        contentPart.addBodyPart(img);

//        ByteArrayDataSource fileds = new ByteArrayDataSource(new FileInputStream("D:\\picture\\jpg\\touxiang.jpg"),"application/octet-stream");
//        DataHandler imgDataHandler = new DataHandler(fileds);
//		DataSource imgds = new FileDataSource("D:\\picture\\jpg\\touxiang.jpg");
//		DataHandler imgDataHandler = new DataHandler(imgds);
//        img.setDataHandler(imgDataHandler);
//        //注意：Content-ID的属性值一定要加上<>，不能是touxiang.jpg
//        img.setHeader("Content-ID", "<touxiang.jpg>");
//        //为图片设置文件名，有的邮箱会把html内嵌的图片也当成附件
//        img.setFileName("touxianga.jpg");
//        //设置文字内容
        /**
         * 注意：在html代码中要想显示刚才的touxiang.jpg
         * src里不能直接写Content-ID的值，要用cid:这种方式
         */
        content.setContent(contentStr, "text/html;charset=utf-8");
        /*正文内容设置结束*/

        /*下面为设置附件*/
        attach1.setDataHandler(new DataHandler(new FileDataSource(fileNamePath)));
        attach1.setFileName(fileName);
//        attach2.setDataHandler(new DataHandler(new FileDataSource("E:\\others\\java.txt")));
//        attach2.setFileName("file2.txt");

        msg.saveChanges();
        //把邮件以文件的形式写入到磁盘
//        OutputStream os = new FileOutputStream("E:\\others\\demo.eml");
//        msg.writeTo(os);
//        os.close();

        Transport trans = session.getTransport();
        trans.connect("smtp.exmail.qq.com", 25, "titans@haotejt.com", "TTHT2577098");
        trans.sendMessage(msg, msg.getAllRecipients());
        trans.close();
    }

}
